home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / DrawSprocketTest / DrawSprocketTest.c
Encoding:
C/C++ Source or Header  |  1996-04-24  |  21.0 KB  |  863 lines  |  [TEXT/MPS ]

  1. #include "Quickdraw.h"
  2. #include "Events.h"
  3. #include "Fonts.h"
  4. #include "Memory.h"
  5. #include "Windows.h"
  6. #include "Menus.h"
  7. #include "TextEdit.h"
  8. #include "Dialogs.h"
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include "math.h"
  14.  
  15. #include "Displays.h"
  16.  
  17. #include "DrawSprocket.h"
  18.  
  19. // Globals---------------------------------------------------------------------------
  20. DisplayObject gDisplayObject;
  21. long gFinalTickCount;
  22. Boolean gAutopilot = false;
  23.  
  24. // Local prototypes------------------------------------------------------------------
  25.  
  26. void
  27. InitToolbox(void);
  28.  
  29. void
  30. EventLoop( void );
  31.  
  32. WindowPtr CreateWindow( void );
  33.  
  34. void
  35. DestroyWindow( WindowPtr win );
  36.  
  37. void TestDrawSprocket( void );
  38. void TestDrawSprocketModes( void );
  39. void TestDrawSprocketGDevices( void );
  40. void TestDrawSprocketDisplays( void );
  41. void DestroyDisplayObjects( void );
  42. void TestDrawSprocketGamma( void );
  43. void TestDrawSprocketBuffering( void );
  44. void DumpDisplayConfig(    DisplayConfig *inConfig );
  45. void IncrementColor( RGBColor *ioColor );
  46.  
  47. void
  48. TestDrawSprocket( void )
  49. {
  50.     char text[10];
  51.  
  52.     printf("type anything to run in autopilot mode, or press <RETURN> to\n");
  53.     printf("run in manual mode: ");
  54.     gets(text);
  55.     if( text[0] )
  56.         gAutopilot = true;
  57.             
  58.     TestDrawSprocketGDevices();
  59.     TestDrawSprocketModes();
  60.     TestDrawSprocketDisplays();
  61.     TestDrawSprocketGamma();
  62.     TestDrawSprocketBuffering();
  63.  
  64.     // cleanup
  65.     DestroyDisplayObjects();
  66.     
  67.     printf("\n\n### testing is complete ###\n");
  68. }
  69.  
  70. void main()
  71. {
  72.     InitToolbox();
  73.  
  74.     TestDrawSprocket();
  75.  
  76. //    EventLoop();
  77. }
  78.  
  79. void
  80. EventLoop( void )
  81. {
  82.     Boolean        done = false;
  83.     WindowPtr    win;
  84.     EventRecord    theEvent;
  85.     WindowPtr    whichWin;
  86.     Point        pt;
  87.     short        winPart;
  88.     Boolean        gotEvent = false;
  89.     QDGlobals*    theQDGlobalsPtr;
  90.  
  91.     win = CreateWindow();
  92.     if( NULL == win )
  93.         return;
  94.         
  95.     SetPort(win);
  96.  
  97.     while ( !done )
  98.     {
  99.         gotEvent = WaitNextEvent( everyEvent, &theEvent, 5, NULL );
  100.         if ( gotEvent || theEvent.what == nullEvent )
  101.         {
  102.             switch (theEvent.what)
  103.             {
  104.             case keyDown:
  105.                 done = true;
  106.                 break;
  107.                 
  108.             case updateEvt:
  109.                 whichWin = (WindowPtr)theEvent.message;
  110.                 if ( whichWin == win )
  111.                 {
  112.                     SetPort(win);
  113.                     BeginUpdate(win);
  114.                     //DrawWindow( win );
  115.                     EndUpdate(win);
  116.                 }
  117.                 break;
  118.             case mouseDown:
  119.                 winPart = FindWindow(theEvent.where, &whichWin);
  120.                 
  121.                 if ( whichWin != win )
  122.                     break;
  123.  
  124.                 if ( whichWin != FrontWindow() ) 
  125.                 {
  126.                     SelectWindow( whichWin );
  127.                 } 
  128.                 else switch ( winPart )
  129.                 {
  130.                 case inGoAway:
  131.                     if (TrackGoAway(whichWin, theEvent.where))
  132.                     {
  133.                         done = true;
  134.                     }
  135.                     break;
  136.                 case inDrag:    
  137.                     DragWindow( whichWin, theEvent.where, &(qd.screenBits.bounds));
  138.                     break;
  139.                 case inContent:
  140.                     pt = theEvent.where;
  141.                     SetPort(whichWin);
  142.                     GlobalToLocal(&pt);
  143.                     break;
  144.                 }
  145.                 break;
  146.             case app4Evt:
  147.                 if (theEvent.message & 0x01000000)
  148.                 {    /* suspend/resume */
  149.                     if (theEvent.message & 0x00000001)
  150.                     {
  151.                         /* add whatever else to resume your application */
  152.                     } else
  153.                     {
  154.                         /* add whatever else to suspend your application */
  155.                     }
  156.                 }
  157.                 break;
  158.             }
  159.         }
  160.     }
  161.     
  162.     DestroyWindow( win );
  163. }
  164.  
  165. void
  166. InitToolbox(void)
  167. {
  168.     MaxApplZone();
  169.     InitGraf(&qd.thePort);
  170.     InitFonts();
  171.     FlushEvents( everyEvent, 0 );
  172.     InitWindows();
  173.     InitMenus();
  174.     TEInit();
  175.     InitDialogs(0L);
  176.     InitCursor();
  177. }
  178.  
  179. WindowPtr
  180. CreateWindow( void )
  181. {
  182.     GDHandle theMainGDevice;
  183.     Rect theBoundsRect;
  184.     WindowPtr theNewWindow;
  185.  
  186.     theMainGDevice = GetMainDevice();
  187.     if( NULL == theMainGDevice )
  188.         return NULL;
  189.  
  190.     theBoundsRect = (**theMainGDevice).gdRect;
  191.     theNewWindow = NewCWindow(\
  192.         NULL,            // wStorage
  193.         &theBoundsRect,    // boundsRect
  194.         NULL,            // title
  195.         TRUE,            // visible
  196.         plainDBox,        // procID
  197.         (WindowPtr)-1,    // behind
  198.         FALSE,            // goAwayFlag
  199.         0);                // refCon
  200.     
  201.     return theNewWindow;
  202. }
  203.  
  204. void
  205. DestroyWindow( WindowPtr win )
  206. {
  207.     DisposeWindow( win );
  208. }
  209.  
  210. void
  211. TestDrawSprocketGDevices( void )
  212. {
  213.     GDHandle theMainGDevice, theRestoredGDevice, theTempGDevice;
  214.     OSErr theError;
  215.  
  216.     printf("\n### Testing GDevices ###\n");
  217.  
  218.     theMainGDevice = GetMainDevice();
  219.     if( NULL == theMainGDevice )
  220.         return;
  221.  
  222.     // duplicate the main gdevice and compare it
  223.     theTempGDevice = theMainGDevice;
  224.     theError = HandToHand( (Handle *)&theTempGDevice );
  225.     if( theError )
  226.     {
  227.         printf("error %d from HandToHand duplicating main GDevice.\n", theError );
  228.         return;
  229.     }
  230.     theError = RestoreSavedGDevice( theTempGDevice, &theRestoredGDevice );
  231.     if( theRestoredGDevice != theMainGDevice )
  232.         printf("RestoreSavedGDevice did not match the main GDevice!\n");
  233.     else
  234.         printf("restored the main GDevice successfully\n");
  235.     DisposeHandle( (Handle)theTempGDevice );
  236.         
  237. }
  238.  
  239. void
  240. TestDrawSprocketModes( void )
  241. {
  242.     UInt32 theModeCount;
  243.     UInt32 theModeIndex;
  244.     OSErr theError;
  245.     DisplayConfig theConfig, theActualConfig;
  246.     GDHandle theGDevice;
  247.     SInt32 theTimingMode, theDepthMode;
  248.  
  249.     theGDevice = GetMainDevice();
  250.     printf("the main GDevice is at 0x%X\n", theGDevice);
  251.     
  252.     printf("\n### Testing Modes ###\n");
  253.  
  254.     theGDevice = DMGetFirstScreenDevice( true );
  255.     while( theGDevice )
  256.     {
  257.         // get the # of timing modes
  258.         theError = GetModeCount( theGDevice, &theModeCount );
  259.         if( theError )
  260.         {
  261.             printf( "error %d from GetModeCount\n", theError );
  262.             return;
  263.         }
  264.         printf( "there are %d modes for the GDevice at 0x%X.\n", theModeCount,
  265.             theGDevice );
  266.         
  267.         // iterate through the modes
  268.         for( theModeIndex = 0; theModeIndex < theModeCount;
  269.             theModeIndex++ )
  270.         {
  271.             theConfig.device = theGDevice;
  272.             theConfig.mode = theModeIndex;
  273.             
  274.             theError = GetModeCapabilities( &theConfig );
  275.             if( theError )
  276.             {
  277.                 printf("Error %d from GetModeCapabilities\n", theError );
  278.                 return;
  279.             }
  280.             printf("mode %d capabilities:\n", theModeIndex );
  281.             DumpDisplayConfig( &theConfig );
  282.         }
  283.         
  284.         // next GDevice
  285.         theGDevice = DMGetNextScreenDevice( theGDevice, true );
  286.     }    
  287.     // find the best configurations
  288.     
  289.     // 320x200x8
  290.     theConfig.width                    = 320;
  291.     theConfig.height                = 200;
  292.     theConfig.specialFlags             = 0;
  293.     theConfig.specialFlagsInHW         = 0;
  294.     theConfig.frontBufferDepthMask    = kDisplayDepthMask8;
  295.     theConfig.backBufferDepthMask    = kDisplayDepthMask8;
  296.     theConfig.frontBufferBestDepth    = 8;
  297.     theConfig.backBufferBestDepth    = 8;
  298.     theConfig.reserved1                = 0;
  299.     theConfig.reserved2                = 0;
  300.     theError = FindGDeviceFromConfig( &theConfig );
  301.     if( theError )
  302.         printf("error %d from FindGDeviceFromConfig\n", theError );
  303.     else
  304.     {
  305.         printf("best match for 320x200x8 is mode %d of GDevice at 0x%X\n", theConfig.mode,
  306.             theConfig.device );
  307.             
  308.         printf("the actual config for the mode is:\n");
  309.         theActualConfig.device = theConfig.device;
  310.         theActualConfig.mode = theConfig.mode;
  311.         GetModeCapabilities( &theActualConfig );
  312.         DumpDisplayConfig( &theActualConfig );
  313.     }
  314.                     
  315.     // 320x240x16
  316.     theConfig.width                    = 320;
  317.     theConfig.height                = 240;
  318.     theConfig.specialFlags             = 0;
  319.     theConfig.specialFlagsInHW         = 0;
  320.     theConfig.frontBufferDepthMask    = kDisplayDepthMask16;
  321.     theConfig.backBufferDepthMask    = kDisplayDepthMask16;
  322.     theConfig.frontBufferBestDepth    = 16;
  323.     theConfig.backBufferBestDepth    = 16;
  324.     theConfig.reserved1                = 0;
  325.     theConfig.reserved2                = 0;
  326.     theError = FindGDeviceFromConfig( &theConfig );
  327.     if( theError )
  328.         printf("error %d from FindGDeviceFromConfig\n", theError );
  329.     else
  330.     {
  331.         printf("best match for 320x240x16 is mode %d of GDevice at 0x%X\n", theConfig.mode,
  332.             theConfig.device );
  333.             
  334.         printf("the actual config for the mode is:\n");
  335.         theActualConfig.device = theConfig.device;
  336.         theActualConfig.mode = theConfig.mode;
  337.         GetModeCapabilities( &theActualConfig );
  338.         DumpDisplayConfig( &theActualConfig );
  339.     }
  340.  
  341.     // 640x480x8
  342.     theConfig.width                    = 640;
  343.     theConfig.height                = 480;
  344.     theConfig.specialFlags             = 0;
  345.     theConfig.specialFlagsInHW         = 0;
  346.     theConfig.frontBufferDepthMask    = kDisplayDepthMask8;
  347.     theConfig.backBufferDepthMask    = kDisplayDepthMask8;
  348.     theConfig.frontBufferBestDepth    = 8;
  349.     theConfig.backBufferBestDepth    = 8;
  350.     theConfig.reserved1                = 0;
  351.     theConfig.reserved2                = 0;
  352.     theError = FindGDeviceFromConfig( &theConfig );
  353.     if( theError )
  354.         printf("error %d from FindGDeviceFromConfig\n", theError );
  355.     else
  356.     {
  357.         printf("best match for 640x480x8 is mode %d of GDevice at 0x%X\n", theConfig.mode,
  358.             theConfig.device );
  359.             
  360.         printf("the actual config for the mode is:\n");
  361.         theActualConfig.device = theConfig.device;
  362.         theActualConfig.mode = theConfig.mode;
  363.         GetModeCapabilities( &theActualConfig );
  364.         DumpDisplayConfig( &theActualConfig );
  365.     }
  366.     
  367.     // 640x480x32 (depth not required)
  368.     theConfig.width                    = 640;
  369.     theConfig.height                = 480;
  370.     theConfig.specialFlags             = 0;
  371.     theConfig.specialFlagsInHW         = 0;
  372.     theConfig.frontBufferDepthMask    = kDisplayDepthMaskAll;
  373.     theConfig.backBufferDepthMask    = kDisplayDepthMaskAll;
  374.     theConfig.frontBufferBestDepth    = 32;
  375.     theConfig.backBufferBestDepth    = 32;
  376.     theConfig.reserved1                = 0;
  377.     theConfig.reserved2                = 0;
  378.     theError = FindGDeviceFromConfig( &theConfig );
  379.     if( theError )
  380.         printf("error %d from FindGDeviceFromConfig\n", theError );
  381.     else
  382.     {
  383.         printf("best match for 640x480x32 (depth not required) is mode %d of GDevice at 0x%X\n",
  384.             theConfig.mode, theConfig.device );
  385.             
  386.         printf("the actual config for the mode is:\n");
  387.         theActualConfig.device = theConfig.device;
  388.         theActualConfig.mode = theConfig.mode;
  389.         GetModeCapabilities( &theActualConfig );
  390.         DumpDisplayConfig( &theActualConfig );
  391.     }
  392.  
  393. }
  394.  
  395. void TestDrawSprocketDisplays( void )
  396. {
  397.     DisplayConfig theConfig, theReturnedConfig;
  398.     UInt32 thePlayState;
  399.     OSErr theError;
  400.     char text[80];
  401.     int theIndex;
  402.     
  403.     printf("\n### Testing Displays ###\n");
  404.     
  405.     // DSp_SetDebugMode( true );
  406.     
  407.     // 640x480x8
  408.     theConfig.width                    = 640;
  409.     theConfig.height                = 480;
  410.     theConfig.specialFlags             = kDisplaySpecialBuffered;
  411.     theConfig.specialFlagsInHW         = 0;
  412.     theConfig.frontBufferDepthMask    = kDisplayDepthMaskAll;
  413.     theConfig.backBufferDepthMask    = kDisplayDepthMaskAll;
  414.     theConfig.frontBufferBestDepth    = 8;
  415.     theConfig.backBufferBestDepth    = 8;
  416.     theConfig.pageCount                = 2;
  417.     theConfig.reserved1                = 0;
  418.     theConfig.reserved2                = 0;
  419.     theConfig.colorTable            = NULL;
  420.     theError = FindGDeviceFromConfig( &theConfig );
  421.     if( theError )
  422.     {
  423.         printf("error %d from FindGDeviceFromConfig, aborting program\n", theError );
  424.         gets(text);
  425.         ExitToShell();
  426.     }
  427.     
  428.     // get the display
  429.     theError = NewDisplay( &theConfig, &gDisplayObject );
  430.     if( theError )
  431.         printf("error %d from NewDisplay\n", theError );
  432.     
  433.     // get the play state
  434.     theError = GetDisplayPlayState( gDisplayObject, &thePlayState );
  435.     if( theError )
  436.     {
  437.         printf("error %d from GetDisplayPlayState()\n", theError );
  438.         return;
  439.     }
  440.     if( kDisplayPlayStateInactive != thePlayState )
  441.     {
  442.         printf("display state should be inactive, but its not.  state = %d\n", thePlayState );
  443.     }
  444.     else
  445.         printf("play state is inactive (correct).\n");
  446.     
  447.     // enter play mode
  448.     if( !gAutopilot )
  449.     {
  450.         printf("press <RETURN> to change dispay modes, then <RETURN> again to come back\n");
  451.         gets(text);
  452.     }
  453.     theError = SetDisplayPlayState( gDisplayObject, kDisplayPlayStateActive );
  454.     if( theError )
  455.     {
  456.         printf("error %d from SetDisplayPlayState(kDisplayPlayStateActive)\n", theError );
  457.         return;
  458.     }
  459.     
  460.     // get the play state
  461.     theError = GetDisplayPlayState( gDisplayObject, &thePlayState );
  462.     if( theError )
  463.     {
  464.         printf("error %d from GetDisplayPlayState()\n", theError );
  465.         return;
  466.     }
  467.     if( kDisplayPlayStateActive != thePlayState )
  468.     {
  469.         printf("display state should be active, but its not.  state = %d\n", thePlayState );
  470.     }
  471.     else
  472.         printf("play state is active (correct).\n");
  473.     
  474.     // wait for a bit
  475.     if( !gAutopilot )
  476.     {
  477.         gets(text);
  478.     }
  479.     
  480.     // exit play mode
  481.     theError = SetDisplayPlayState( gDisplayObject, kDisplayPlayStateInactive );
  482.     if( theError )
  483.     {
  484.         printf("error %d from SetDisplayPlayState(kDisplayPlayStateInactive)\n", theError );
  485.         return;
  486.     }
  487.     
  488.     // get the play state
  489.     theError = GetDisplayPlayState( gDisplayObject, &thePlayState );
  490.     if( theError )
  491.     {
  492.         printf("error %d from GetDisplayPlayState()\n", theError );
  493.         return;
  494.     }
  495.     if( kDisplayPlayStateInactive != thePlayState )
  496.     {
  497.         printf("display state should be inactive, but its not.  state = %d\n", thePlayState );
  498.     }
  499.     else
  500.         printf("play state is inactive (correct).\n");
  501.     
  502.     // watch the frame count for a little bit
  503.     for( theIndex = 0; theIndex < 10; theIndex++ )
  504.     {
  505.         UInt32 theFrame;
  506.         
  507.         theFrame = GetDisplayFrameCount( gDisplayObject );
  508.         printf("current display frame is %d\n", theFrame);
  509.         Delay(10, &gFinalTickCount);
  510.     }
  511.     
  512.     // get the requested config
  513.     theError = GetDisplayConfig( gDisplayObject, &theReturnedConfig );
  514.     if( theError )
  515.     {
  516.         printf("error %d from GetDisplayConfig()\n", theError );
  517.         return;
  518.     }
  519.     
  520.     // dump our config
  521.     printf("True Requested Config:\n");
  522.     DumpDisplayConfig( &theConfig );
  523.     
  524.     // dump requested config from display object
  525.     printf("Returned Requested Config:\n");
  526.     DumpDisplayConfig( &theReturnedConfig );
  527.     
  528.     // get the actual config
  529.     theError = GetDisplayActualConfig( gDisplayObject, &theReturnedConfig );
  530.     if( theError )
  531.     {
  532.         printf("error %d from GetDisplayConfig()\n", theError );
  533.         return;
  534.     }
  535.     
  536.     // dump actual config
  537.     printf("Returned Actual Config:\n");
  538.     DumpDisplayConfig( &theReturnedConfig );
  539.     
  540. }
  541.  
  542. void DumpDisplayConfig(
  543.     DisplayConfig *inConfig
  544. )
  545. {
  546.     printf("\tdevice                = 0x%0X\n", inConfig->device );
  547.     printf("\tmode                  = 0x%0X\n", inConfig->mode );
  548.     printf("\tfrequency             = 0x%0X\n", inConfig->frequency );
  549.     printf("\twidth                 = %d\n", inConfig->width );
  550.     printf("\theight                = %d\n", inConfig->height );
  551.     printf("\tcolorNeeds            = 0x%0X\n", inConfig->colorNeeds );
  552.     printf("\tcolorTable            = 0x%0X\n", inConfig->colorTable );
  553.     printf("\tspecialFlags          = 0x%0X\n", inConfig->specialFlags );
  554.     printf("\tspecialFlagsInHW      = 0x%0X\n", inConfig->specialFlagsInHW );
  555.     printf("\tbackBufferDepthMask   = 0x%0X\n", inConfig->backBufferDepthMask );
  556.     printf("\tfrontBufferDepthMask  = 0x%0X\n", inConfig->frontBufferDepthMask );
  557.     printf("\tbackBufferBestDepth   = %d\n", inConfig->backBufferBestDepth );
  558.     printf("\tfrontBufferBestDepth  = %d\n", inConfig->frontBufferBestDepth );
  559.     printf("\tpageCount             = 0x%0X\n", inConfig->pageCount );
  560.     printf("\treserved1             = 0x%0X\n", inConfig->reserved1 );
  561.     printf("\treserved2             = 0x%0X\n", inConfig->reserved2 );
  562. }
  563.  
  564. void DestroyDisplayObjects( void )
  565. {
  566.     OSErr theError;
  567.     
  568.     if( gDisplayObject )
  569.     {
  570.         theError = DisposeDisplay( gDisplayObject );
  571.         if( theError )
  572.             printf("error %d from DisposeDisplay\n", theError );
  573.         gDisplayObject = NULL;
  574.     }
  575. }
  576.  
  577. void
  578. TestDrawSprocketGamma( void )
  579. {
  580.     OSErr theError;
  581.     char text[10];
  582.     RGBColor theColor;
  583.     
  584.     printf("\n\n### testing gamma ###\n");
  585.  
  586.     if( !gAutopilot )
  587.     {
  588.         printf("press <RETURN>\n");
  589.         gets(text);
  590.     }
  591.     
  592.     // fade to black, all display objects
  593.     theError = FadeDisplayGamma( NULL, kSmoothFadeOut, NULL );
  594.     if( theError )
  595.     {
  596.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  597.         printf("error %d from FadeDisplayGamma\n", theError );
  598.         return;
  599.     }
  600.     
  601.     // activate the display object
  602.     SetDisplayPlayState( gDisplayObject, kDisplayPlayStateActive );
  603.     Delay(30, &gFinalTickCount);
  604.     
  605.     // bring the display back in
  606.     theError = FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  607.     if( theError )
  608.     {
  609.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  610.         printf("error %d from FadeDisplayGamma\n", theError );
  611.         return;
  612.     }
  613.     
  614.     if( !gAutopilot )
  615.     {
  616.         printf("press <RETURN>\n");
  617.         gets(text);
  618.     }
  619.     
  620.     // fade to color, only one display object
  621.     theColor.red = 0xffff;
  622.     theColor.green = 0;
  623.     theColor.blue = 0;
  624.         
  625.     theError = FadeDisplayGamma( gDisplayObject, kSmoothFadeOut, &theColor );
  626.     if( theError )
  627.     {
  628.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  629.         printf("error %d from FadeDisplayGamma\n", theError );
  630.         return;
  631.     }
  632.     
  633.     if( !gAutopilot )
  634.     {
  635.         printf("press <RETURN>\n");
  636.         gets(text);
  637.     }
  638.     
  639.     // fade back in
  640.     theError = FadeDisplayGamma( gDisplayObject, kSmoothFadeIn, &theColor );
  641.     if( theError )
  642.     {
  643.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  644.         printf("error %d from FadeDisplayGamma\n", theError );
  645.         return;
  646.     }
  647.  
  648.     if( !gAutopilot )
  649.     {
  650.         printf("press <RETURN>\n");
  651.         gets(text);
  652.     }
  653.     
  654.     // fade to black, all display objects
  655.     theError = FadeDisplayGamma( NULL, kSmoothFadeOut, NULL );
  656.     if( theError )
  657.     {
  658.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  659.         printf("error %d from FadeDisplayGamma\n", theError );
  660.         return;
  661.     }
  662.     
  663.     // inactivate the display object
  664.     SetDisplayPlayState( gDisplayObject, kDisplayPlayStateInactive );
  665.     Delay(30, &gFinalTickCount);
  666.     
  667.     // bring the display back in
  668.     theError = FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  669.     if( theError )
  670.     {
  671.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  672.         printf("error %d from FadeDisplayGamma\n", theError );
  673.         return;
  674.     }
  675. }
  676.  
  677. void TestDrawSprocketBuffering( void )
  678. {
  679.     GWorldPtr theGWorld;
  680.     OSStatus theError;
  681.     char text[10];
  682.     PixMapHandle thePixMap;
  683.     UInt32 theCount;
  684.     Rect theRect, theRectangleRect;
  685.     Str255 theText;
  686.     DisplayConfig theConfig;
  687.     UInt32 theMaxTick, theStartTick, theCurrentTick, theWidth;
  688.     SInt32 theXPos, theYPos;
  689.     RGBColor theColor, theTextColor;
  690.     SInt32 theRectangleDelta;
  691.     GDHandle theOldGDevice;
  692.     GWorldPtr theOldGWorld;
  693.     
  694.     printf("\n\n### testing buffering ###\n");
  695.     if( !gAutopilot )
  696.     {
  697.         printf("During these tests the console window will not be visible!\n");
  698.         printf("Just watch the display, you should see a swap every 1/2 second.\n\n");
  699.         printf("press <RETURN> to begin\n");
  700.         gets(text);
  701.     }
  702.  
  703.     // DSp_SetDebugMode( true );
  704.     
  705.     // get the display attributes (the ones I think they are, not necessarily
  706.     // the real ones)        
  707.     GetDisplayConfig( gDisplayObject, &theConfig );
  708.  
  709.     // fade to black, all display objects
  710.     theError = FadeDisplayGamma( NULL, kSmoothFadeOut, NULL );
  711.     if( theError )
  712.     {
  713.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  714.         printf("error %d from FadeDisplayGamma\n", theError );
  715.         return;
  716.     }
  717.     
  718.     // activate the display object
  719.     SetDisplayPlayState( gDisplayObject, kDisplayPlayStateActive );
  720.     Delay(30, &gFinalTickCount);
  721.     
  722.     // bring the display back in
  723.     theError = FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  724.     if( theError )
  725.     {
  726.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  727.         printf("error %d from FadeDisplayGamma\n", theError );
  728.         return;
  729.     }
  730.     
  731.     GetGWorld( &theOldGWorld, &theOldGDevice );
  732.  
  733.     // do the double buffering
  734.     theColor.red = 0;
  735.     theColor.green = 0;
  736.     theColor.blue = 0;
  737.     theTextColor.red = 0xFFFF;
  738.     theTextColor.green = 0xFFFF;
  739.     theTextColor.blue = 0xFFFF;
  740.     
  741.     // rectangle will be 1/8 the display width, and all of the height
  742.     SetRect( &theRectangleRect, 0, 0, theConfig.width >> 3, theConfig.height );
  743.     
  744.     theStartTick = theCurrentTick = TickCount();
  745.     theMaxTick = theStartTick + (15 * 60);
  746.     theCount = 0;
  747.     theRectangleDelta = 3;
  748.     while( theCurrentTick < theMaxTick )
  749.     {
  750.         // get the back buffer
  751.         theError = GetDisplayBackBuffer( gDisplayObject, &theGWorld );
  752.         if( theError )
  753.         {
  754.             printf("error %d from GetDisplayBackBuffer\n", theError );
  755.             return;
  756.         }
  757.         
  758.         // set the current gworld
  759.         SetGWorld( theGWorld, NULL );
  760.         
  761.         // clear the buffer
  762.         EraseRect( &theGWorld->portRect );
  763.  
  764.         // fill the display with sliding vertical rectangle
  765.         IncrementColor( &theColor );
  766.         RGBForeColor( &theColor );        
  767.         PaintRect( &theRectangleRect );
  768.         
  769.         if( ( (theRectangleRect.right + theRectangleDelta) > theConfig.width ) ||
  770.             ( (theRectangleRect.left + theRectangleDelta) < 0 ) )
  771.         {
  772.             theRectangleDelta = -theRectangleDelta;
  773.         }
  774.         theRectangleRect.left += theRectangleDelta;
  775.         theRectangleRect.right += theRectangleDelta;
  776.  
  777.         // draw a frame counter
  778.         sprintf((char *)&theText[1], "This is frame %d (%d ticks remaining, %.2f fps)",
  779.             theCount + 1, theMaxTick - theCurrentTick,
  780.             (float)(theCount / ((theCurrentTick - theStartTick) / 60.0)) );
  781.         theText[0] = strlen( (char *)&theText[1] );
  782.         theWidth = StringWidth( (ConstStr255Param)theText );
  783.         theXPos = ( theConfig.width >> 1 ) - ( theWidth >> 1 );
  784.         theYPos = theConfig.height - 20;
  785.  
  786.         theTextColor.red = 0;
  787.         theTextColor.green = 0;
  788.         theTextColor.blue = 0;
  789.         RGBForeColor( &theTextColor );
  790.         SetRect( &theRect, theXPos - 10, theYPos - 20, theXPos + theWidth + 10, theYPos + 10 );
  791.         PaintRect( &theRect );
  792.  
  793.         theTextColor.red = 0;
  794.         theTextColor.green = 0xFFFF;
  795.         theTextColor.blue = 0xFFFF;
  796.         RGBForeColor( &theTextColor );
  797.         FrameRect( &theRect );
  798.         
  799.         MoveTo( theXPos, theYPos );
  800.         DrawString( (ConstStr255Param)theText );
  801.         
  802.         // swap the buffers
  803.         theError = SwapDisplayBuffers( gDisplayObject, NULL, 0 );
  804.         if( theError )
  805.         {
  806.             printf("error %d from SwapDisplayBuffers\n", theError );
  807.             return;
  808.         }
  809.         
  810.         // increase frame counter
  811.         theCount++;
  812.         theCurrentTick = TickCount();
  813.     }
  814.  
  815.     SetGWorld( theOldGWorld, theOldGDevice );
  816.     
  817.     // fade to black, all display objects
  818.     theError = FadeDisplayGamma( NULL, kSmoothFadeOut, NULL );
  819.     if( theError )
  820.     {
  821.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  822.         printf("error %d from FadeDisplayGamma\n", theError );
  823.         return;
  824.     }
  825.     
  826.     // inactivate the display object
  827.     SetDisplayPlayState( gDisplayObject, kDisplayPlayStateInactive );
  828.     Delay(30, &gFinalTickCount);
  829.     
  830.     // bring the display back in
  831.     theError = FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  832.     if( theError )
  833.     {
  834.         FadeDisplayGamma( NULL, kSmoothFadeIn, NULL );
  835.         printf("error %d from FadeDisplayGamma\n", theError );
  836.         return;
  837.     }
  838. }
  839.  
  840. void
  841. IncrementColor( RGBColor *ioColor )
  842. {
  843.     // increment red
  844.     ioColor->red += 0x0100;
  845.     if( ioColor->red >= 0xFE00 )
  846.     {
  847.         ioColor->red = 0;
  848.         
  849.         // increment green
  850.         ioColor->green += 0x0100;
  851.         if( ioColor->green >= 0xFE00 )
  852.         {
  853.             ioColor->green = 0;
  854.             
  855.             // increment blue
  856.             ioColor->blue += 0x0100;
  857.             if( ioColor->blue >= 0xFE00 )
  858.             {
  859.                 ioColor->blue = 0;
  860.             }
  861.         }
  862.     }
  863. }